package com.applink.sell_proof.builder;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

import android.content.Context;
import android.content.Intent;
import android.text.format.DateFormat;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TableRow;
import android.widget.TextView;

import com.applink.sell_proof.DetailClaim;
import com.applink.sell_proof.R;
import com.applink.sell_proof.model.Claim;
import com.applink.sell_proof.util.Utility;

public class AllClaimBuilder extends AbstractListBuilder implements
		OnClickListener {

	private int mWidth;
	protected int mSelectedPosition = -1;
	private long idClaim = -1;
	private static ArrayList<Long> idCLaimOld = new ArrayList<Long>();

	public AllClaimBuilder(Context context, ViewGroup parent) {
		super(context, parent);

		mWidth = mContext.getResources().getDisplayMetrics().widthPixels;

		mWidth = mWidth + mWidth / 3;
	}

	@Override
	public void notifyDataChange() {
		mAdapter.notifyDataSetChanged();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.header_deal_item:
			int position = (Integer) v.getTag();
			Claim claim = (Claim) mData.get(position);
			Intent intent = new Intent(mContext, DetailClaim.class);

			// intent.putExtra(DetailClaim.CLAIM_ID, claim.getId());

			intent.putExtra(DetailClaim.CLAIM_LOCALID, claim.getLocalId());
			intent.putExtra(DetailClaim.CLAIM_STATUS, claim.getStatus());

			mContext.startActivity(intent);
			break;
		case R.id.table_row_deal:
			position = (Integer) v.getTag();
			int firstItem = mListView.getFirstVisiblePosition()
					- mListView.getHeaderViewsCount();
			int real_position = position - firstItem;
			if (real_position >= 0 && real_position < mListView.getChildCount()) {
				View layout_item = mListView.getChildAt(real_position);
				View menu = layout_item.findViewById(R.id.deal_menu);
				menu.setVisibility(menu.getVisibility() == View.VISIBLE ? View.GONE
						: View.VISIBLE);
				mSelectedPosition = menu.getVisibility() == View.VISIBLE ? position
						: -1;
				notifyDataChange();
			}
			break;
		default:
			break;
		}

	}

	@Override
	public View buildConvertView() {
		View layout = mLayoutInflater.inflate(R.layout.opportunity_item, null);
		TableRow tableRow = (TableRow) layout.findViewById(R.id.table_row_deal);
		tableRow.setOnClickListener(this);
		layout.findViewById(R.id.header_deal_item).setOnClickListener(this);
		for (int i = 0; i < tableRow.getChildCount(); i++) {
			View child = tableRow.getChildAt(i);
			child.getLayoutParams().width = mWidth / 3;
		}
		return layout;
	}

	@Override
	public ConcurrentHashMap<Integer, View> buildConvertViewContent(View base) {

		ConcurrentHashMap<Integer, View> views = new ConcurrentHashMap<Integer, View>();

		views.put(R.id.table_row_deal, base.findViewById(R.id.table_row_deal));

		views.put(R.id.deal_menu, base.findViewById(R.id.deal_menu));

		views.put(R.id.invoice_no, base.findViewById(R.id.invoice_no));

		views.put(R.id.claim_status, base.findViewById(R.id.claim_status));

		views.put(R.id.customer_name, base.findViewById(R.id.customer_name));

		views.put(R.id.claim_type, base.findViewById(R.id.claim_type));

		views.put(R.id.close_date, base.findViewById(R.id.close_date));

		views.put(R.id.header_deal_item,
				base.findViewById(R.id.header_deal_item));
		return views;
	}
	
	private  boolean checkExist(long id){
		boolean isExist = false;
		
		for(int i=0; i< idCLaimOld.size();i++){
			if(idCLaimOld.get(i) == id){
				return true;
			}
		}
		return isExist;
	}


	@Override
	public void builConvertViewContentData(int position,
			ConcurrentHashMap<Integer, View> views, List<? extends Object> data) {
	
		Claim claim = (Claim) mData.get(position);
		//boolean checkID = checkExist(claim.getId());
	//	if(checkID == false){
		//	if(idClaim != claim.getId()){
	//		idCLaimOld.add(claim.getId());
			//idClaim = claim.getId();
				View datarow = views.get(R.id.table_row_deal);
				datarow.setBackgroundColor(mContext.getResources().getColor(
						Utility.getStatusBGColor(claim.getStatus())));

				((TextView) views.get(R.id.invoice_no)).setText(claim.getInvoiceNo());

				((TextView) views.get(R.id.claim_status)).setText(claim.getStatus());

				((TextView) views.get(R.id.customer_name)).setText(claim
						.getCustomerCompanyName());

				((TextView) views.get(R.id.claim_type)).setText(claim.getType());

				String date = DateFormat.getDateFormat(mContext).format(
						new Date(claim.getDate()));
				((TextView) views.get(R.id.close_date)).setText(date);

				views.get(R.id.table_row_deal).setTag(position);
				views.get(R.id.header_deal_item).setTag(position);
				views.get(R.id.deal_menu).setVisibility(
						mSelectedPosition == position ? View.VISIBLE : View.GONE);
		//	}
			
//		}
	}

}
